<template>
  <div>
    <el-table
      :data="GoodsList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column type="index" label="商品序号" >
      </el-table-column>
      <el-table-column prop="id" label="商品编号" >
      </el-table-column>
      <el-table-column prop="goodsname" label="商品名称" >
      </el-table-column>
      <el-table-column prop="price" label="商品价格"> </el-table-column>
      <el-table-column prop="market_price" label="市场价格"> </el-table-column>
      <el-table-column  label="图片"> 
          <template v-slot="scoped">
          <div>
            <img :src="$pre + scoped.row.img" alt="">
          </div>
        </template>
      </el-table-column>
      <el-table-column  label="是否新品">
        <template v-slot="scoped">
          <div>
            <el-button type="success" v-if="scoped.row.isnew == 1">是</el-button>
            <el-button type="info" v-else>否</el-button>
          </div>
        </template> </el-table-column>
      <el-table-column label="是否热卖"> 
        <template v-slot="scoped">
          <div>
            <el-button type="success" v-if="scoped.row.ishot == 1">是</el-button>
            <el-button type="info" v-else>否</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column  label="状态"> 
        <template v-slot="scoped">
          <div>
            <el-button type="success" v-if="scoped.row.status == 1">启 用</el-button>
            <el-button type="info" v-else>禁 用</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作"> 
        <template v-slot="scoped">
          <div>
            <el-button @click="update(scoped.row.id)">编 辑</el-button>
            <el-button @click="del(scoped.row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
<el-pagination background layout="prev, pager, next" :total="goodsTotal" :page-size="goodsSize" @current-change="changePage">
</el-pagination>


  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {goodsDel} from "../../../request/api";
import { sucessAlert, warningAlert } from '../../../utils/alert.js';
export default {
  props: [],
  components: {},
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      GoodsList:"goods/list",
       goodsTotal: "goods/total",
      goodsSize: "goods/size"
    }),
  },
  methods: {
    ...mapActions({
      GoodsAsyncList:"goods/asyncList",
       asyncGoodsTotal: "goods/asyncTotal",
      asyncGoodsPage: "goods/asyncPage"
    }),
    //清空
    del(id){
      goodsDel({id:id}).then(res=>{
         if (res.data.code == 200) {
          // 成功的提示窗
          sucessAlert(res.data.msg);
          // 刷新列表数据
          this.GoodsAsyncList();
          // 重新请求列表总条数
          this.asyncGoodsTotal();
        } else {
          // 失败的提示窗
          warningAlert(res.data.msg);
        }
      })
    },
    //编辑
    update(id){
      this.$emit("update",id);
    },
    changePage(e){
      this.asyncGoodsPage(e)
    }
  },
  mounted() {
    this.GoodsAsyncList(),
    this.asyncGoodsTotal(),
    this.asyncGoodsPage(1)
  },
};
</script>
<style scoped>
img{
  width: 150px;
  height:150px;
}
</style>